#!usr/bin/python3
#-*- coding:utf -8-*-


import pymysql
from lxml import etree
from getContent import GetContent
from db import Db
from urllib.request import urlretrieve
import time
import os,stat
import re


host = 'http://www.xiachufang.com'
dbC = Db()
db = dbC.dbConfig()
cursor = db.cursor(cursor=pymysql.cursors.DictCursor)
getContentC = GetContent()

def go():
    #获取分类
    try:
        sql = 'select * from category'
        cursor.execute(sql)
        catList = cursor.fetchall()
        
        for cat in catList:
            content = getContentC.go(cat['url'])
            if content:
                html = etree.HTML(content)
                try:
                    foodList = html.xpath('//div[@class="dishes-280-full-width-list"]/ul[1]/li')
                    if not foodList:
                        continue
                    for food in foodList:
                        try:
                            foodName = food.xpath('./child::div[1]/p[1]/a[1]/text()')[0]
                            foodDetailUrl = food.xpath('./child::div[1]/p[1]/a[1]/@href')[0]
                            foodDesc = food.xpath('./child::div[1]/p[2]/text()')[0]
                            foodGood = food.xpath('./child::div[1]/div[2]/p[2]/span[1]/text()')[0]
                            foodCover = food.xpath('./child::div[1]/div[1]/img/@data-src')[0]
                            memberName = food.xpath('./child::div[1]/div[3]/a[1]/@title')[0]
                            memberImg = food.xpath('./child::div[1]/div[3]/a[1]/img/@data-src')[0]
                            memberAddress = food.xpath('./child::div[1]/div[3]/span[1]/text()')[0]
                            
                            addrReg = re.compile(r'（(.*?)）',re.S)
                            memberAddress = re.search(addrReg,memberAddress).group(1)

                            #获取详情页
                            foodDetailUrl = '{0}/{1}'.format(host,foodDetailUrl)
                            detailContent = getContentC.go(foodDetailUrl)
                            if detailContent:
                                try:
                                    dHtml = etree.HTML(detailContent)
                                    foodPublicTimeEle = dHtml.xpath('//div[@class="pure-u dish-user-info"]/div[3]')[0]
                                    foodPublicTimeEle = etree.tostring(foodPublicTimeEle,encoding='utf-8').decode('utf-8')
                                    
                                    timeReg = re.compile(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}')
                                    foodPublicTime = re.search(timeReg,foodPublicTimeEle).group(0)
                                    
                                    
                                except Exception as e:
                                    print(e)
                                    # print('食物详情页获取失败:'+foodDetailUrl)
                                    continue
                            else:
                                continue                           
                            exit()
                        except Exception as e:
                            print('获取食物信息'+cat['url'])
                            continue
                except Exception as e:
                    print('获取食物列表失败'+cat['url'])
                    continue
            else:
                continue

    except Exception as e:
        print('查询分类失败')

def downloadPic(url,imgpath):
    if imgpath:
        try:
            urlretrieve(url,imgpath)
        except Exception as e:
            print('下载失败')
            imgPath = ''
        return imgpath
    else:
        pass

if __name__ == '__main__':
    go()


